package com.wfy.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @Title: PxxCorsConfig
 * @Package com.wfy.config
 * @Author 王飞遥
 * @Date 2022/8/17 11:42
 */
@Configuration
public class PxxCorsConfig {

    @Bean
    public CorsFilter corsFilter(){
        //1.添加cors的配置信息
        CorsConfiguration config = new CorsConfiguration();
        //2.允许的域，设置域名
        config.addAllowedOrigin("http://manage.pxx.com:81");
        config.addAllowedOrigin("http://www.pxx.com:81");
        config.setAllowCredentials(true);
        //3.允许的请求方式
        config.addAllowedMethod("OPTIONS");
        config.addAllowedMethod("HEAD");
        config.addAllowedMethod("GET");
        config.addAllowedMethod("PUT");
        config.addAllowedMethod("POST");
        config.addAllowedMethod("DELETE");
        config.addAllowedMethod("PATCH");
        //4.允许的头信息
        config.addAllowedHeader("*");
        //5.添加映射的路径。我们拦截的请求
        UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
        configurationSource.registerCorsConfiguration("/**",config);
        //6.返回一个新的corsFilter
        return new CorsFilter(configurationSource);
    }

}